package java6703.lesson01.shopping;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.Labeled;
import javafx.scene.control.TextField;
import javafx.scene.layout.*;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;
import java6703.lesson01.*;

import java6703.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;


public class ShoppingWin6703 extends Application {
    /**
     * tfPerson  购买者
     * tfBuy     购买的商品
     * lblGet    输出结果
     */


    private TextField tfPerson;
    private TextField tfBuy;
    private Label lblGet;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        VBox root = new VBox(20);

        Label lbStudent = new Label("212106703 陈杰");
        BorderStroke bs = new BorderStroke(Paint.valueOf("#8FBC8F")
                , BorderStrokeStyle.DOTTED
                , new CornerRadii(10)
                , new BorderWidths(3));

        lbStudent.setBorder(new Border(bs));
        lbStudent.setPadding(new Insets(10));

        HBox hPerson = new HBox(10);
        Label lblPerson = new Label("购物者：");


        this.tfPerson = new TextField();
        tfPerson.setPromptText("购物者姓名");
        hPerson.getChildren().addAll(lblPerson, tfPerson);

        HBox hBuy = new HBox(10);
        Label lblBuy = new Label("购买商品清单：");
        this.tfBuy = new TextField();
        tfBuy.setPrefWidth(420);
        tfBuy.setPromptText("用逗号分隔的商品名称");
        hBuy.getChildren().addAll(lblBuy, tfBuy);

        Button button = new Button("购买");
        this.lblGet = new Label();

        root.getChildren().addAll(lbStudent, hPerson, hBuy, button, lblGet);
        root.setPadding(new Insets(20.0));
        root.setAlignment(Pos.TOP_CENTER);


        Scene scene = new Scene(root, 600, 400);
        primaryStage.setScene(scene);
        primaryStage.setTitle("超市购物");
        primaryStage.show();


        EventHandler<ActionEvent> lister = new MyEvenHandler();
        button.setOnAction(lister);
    }


//        try {
//            list = Files.lines(Paths.get("01Lesson6703/java6703/lesson01/Shopping6703")).toArray();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }


        //实现监听接口，在事件处理方法中完成任务
        class MyEvenHandler implements EventHandler<ActionEvent> {

            @Override
            public void handle(ActionEvent event) {
                //超市商品列表
                Product6703[] product6703List = {new Product6703("豆浆机"),
                        new Product6703("吹风机"),
                };

                //创建超市对象
                Market6703 market6703 = new Market6703("永辉超市", product6703List);

                //创建购物者对象
                Person6703 person6703 = new Person6703(tfPerson.getText());

                String s1 = tfBuy.getText();
                //切割
                String[] s2 = s1.split(",");

                Product6703[] product6703s = new Product6703[s2.length];

                //比较清单购物
                for (int i = 0; i < s2.length; i++) {
                    product6703List[i] = new Product6703(s2[i]);
                }

                //购物
                person6703.shopping(market6703, product6703List);

                lblGet.setText(person6703.getName() + "在永辉超市买到了:" + person6703.shopping(market6703, product6703List));

            }
        }
    }


